﻿using ViewMaker.Core.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ViewMaker.Core;
using System.Collections.Generic;
using ViewMaker.Core.Tests.Controls;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;

namespace ViewMaker.Core.Tests
{
    [TestClass()]
    public class DataGridComboBoxColumnViewControlTest
    {
        [TestMethod()]
        public void 標準的なDataGridComboBoxColunmのXAML生成()
        {
            var grid = new DataGridViewControl { Item = new ViewLayoutItem { Name = "gd001", BindPath = "data" } };
            grid.Item.Element = grid;
            var column = new ViewLayoutItem { Name = "col001", Caption = "カラム", BindPath = "currentValue" };
            var target = new DataGridComboBoxColumnViewElement
            {
                Width = 100,
                ItemsSource = "itemsSourcePath",
            };
            column.Element = target;
            grid.Item.AddChild(column);
            var xaml = grid.CreateXaml(TestHelper.CreateTestParent())[0];
            var gridActual = TestHelper.GetFrameworkElement<DataGrid>(xaml);
#if !SILVERLIGHT
            var actual = (DataGridComboBoxColumn)gridActual.Columns[0];
            Assert.AreEqual(100, actual.Width.Value);
            Assert.AreEqual("currentValue", ((Binding)actual.SelectedItemBinding).Path.Path);

            var columnSetter = (Setter)actual.ElementStyle.Setters[0];
            Assert.AreEqual("itemsSourcePath", ((Binding)columnSetter.Value).Path.Path);
#else
            var actual = (DataGridTemplateColumn)gridActual.Columns[0];
            Assert.AreEqual(100, actual.Width.Value);            
#endif

        }


        [TestMethod()]
        public void 表示・値カラムを指定したDataGridComboBoxColunmのXAML生成()
        {
            var grid = new DataGridViewControl { Item = new ViewLayoutItem { Name = "gd001", BindPath = "data" } };
            grid.Item.Element = grid;
            var column = new ViewLayoutItem { Name = "col001", Caption = "カラム", BindPath = "currentValue" };
            var target = new DataGridComboBoxColumnViewElement
            {
                Width = 100,
                ItemsSource = "itemsSourcePath",
                DisplayMemberPath = "displayPath",
                SelectedValuePath = "valuePath",
            };
            column.Element = target;
            grid.Item.AddChild(column);
            var xaml = grid.CreateXaml(TestHelper.CreateTestParent())[0];
            var gridActual = TestHelper.GetFrameworkElement<DataGrid>(xaml);
#if !SILVERLIGHT
            var actual = (DataGridComboBoxColumn)gridActual.Columns[0];
            Assert.AreEqual(100, actual.Width.Value);
            Assert.AreEqual("currentValue", ((Binding)actual.SelectedValueBinding).Path.Path);
            
            var columnSetter = (Setter)actual.ElementStyle.Setters[0];
            Assert.AreEqual("itemsSourcePath", ((Binding)columnSetter.Value).Path.Path);
#else
            var actual = (DataGridTemplateColumn)gridActual.Columns[0];
            Assert.AreEqual(100, actual.Width.Value);            
#endif
        }

        [TestMethod()]
        public void DataGridComboBoxColumnViewControlの編集画面が表示可能()
        {
            var target = new DataGridComboBoxColumnViewElement
            {
                Width = 100,
                ItemsSource = "itemsSourcePath",
            };
            var baml = ViewUtil.BuildContent(target);
            Assert.IsNotNull(baml);
        }

    }
}
